package com.demo.dao;

import com.demo.entity.Person;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import java.util.List;

public class PersonDao{
	
	private SessionFactory sessionFactory;
	
	//增加方法
	public void savePerson(Person person) {
		this.sessionFactory.getCurrentSession().save(person);
		 }
	//删除方法
	public void deletePerson(Person person) {
		this.sessionFactory.getCurrentSession().delete(person);
		 }
	//修改方法
	public void updatePerson(Person person) {
		this.sessionFactory.getCurrentSession().update(person);
		 }
	//列出所有数据库数据方法
	public List<Person> queryPerson() {
		String sql = "select * from person";
		SQLQuery sqlQuery = this.sessionFactory.getCurrentSession().createSQLQuery(sql);
		sqlQuery.addEntity(Person.class);
		return sqlQuery.list();
	}
	//通过id查询返回数据方法
	public Person findPersonById(Integer id) {
		  Person person= (Person)this.sessionFactory.getCurrentSession().get(Person.class,id);
		  return person;
		 }
	
	//生成get跟set方法
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
}
